#coding=utf-8
from flask import render_template, redirect, request, url_for, flash
from flask_login import login_user,logout_user, login_required,current_user
from app.auth import auth
from app.auth.forms import Loginform,RegistrationForm,ChangePasswordForm
from app.models import User
from app import db

@auth.before_app_request
def before_request():
	if current_user.is_authenticated:
		current_user.ping()
	pass


@auth.route('/login',methods=['GET', 'POST'])
def login():
	form=Loginform()
	if form.validate_on_submit():
		user=User.query.filter_by(username=form.username.data).first()
		if user is not None and user.verify_password(form.password.data):
			login_user(user,form.remember_me.data)
			next = request.args.get('next')
			if next is None or not next.startswith('/'):
				next = url_for('main.index')
			return redirect(next)
		flash('Invalid username or password.')
	return render_template('auth/login.html',form=form)

@auth.route('/logout')
@login_required
def logout():
	logout_user()
	flash('You have been logged out.')
	return redirect(url_for('main.index'))

@auth.route('/register', methods=['GET', 'POST'])
def register():
	form = RegistrationForm()
	if form.validate_on_submit():
		user=User(username=form.username.data,password=form.password.data,role_id=2)
		db.session.add(user)
		db.session.commit()
		flash('You can now login.')
		return redirect(url_for('auth.login'))
	return render_template('auth/register.html', form=form)


@auth.route('/change-password', methods=['GET', 'POST'])
@login_required
def change_password():
    form = ChangePasswordForm()
    if form.validate_on_submit():
        if current_user.verify_password(form.old_password.data):
            current_user.password = form.password.data
            db.session.add(current_user)
            db.session.commit()
            flash('用户密码已修改')
            logout_user()
            return redirect(url_for('main.index'))
        else:
            flash('输入密码错误')
    return render_template("auth/change_password.html", form=form)


